package cetcbigdata.da.personal_service.gansu;

import cetcbigdata.da.base.Base;
import cn.wanghaomiao.xpath.model.JXDocument;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static cetcbigdata.da.personal_service.gansu.GSpersonUtil.*;

public class GSpersonal extends Base {
    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {

    }

    @Override
    public JSONObject getDataDemo() {
        try {
            return parseListPage();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    private JSONObject parseListPage() {
        //地区遍历
        StringBuilder region = new StringBuilder();
        region.append("甘肃省-");
        //市
        String SJUrl = "https://zwfw.gansu.gov.cn/szgs/navigationInterface/searchNavigationByCode.do?areaCode=620000000000";
        JSONObject SJHeader = new JSONObject();
        SJHeader.put("Referer", "https://zwfw.gansu.gov.cn/szgs/sxcx/personal/theme/showIndex.do");
        SJHeader.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
        String SJStrResponse = getReq(SJUrl,SJHeader,new JSONObject());
        JSONObject SJResponse = JSONObject.parseObject(SJStrResponse);
        for (Object SJReqParmas_ : SJResponse.getJSONObject("params").getJSONArray("navigationList")){
            JSONObject SJReqParmas = (JSONObject) SJReqParmas_;
            region.replace(4,region.length(),SJReqParmas.getString("name")).append("-");
            int SSLength = region.length();
            //区
            String QJUrl = "https://zwfw.gansu.gov.cn/szgs/navigationInterface/searchNavigationByCode.do?areaCode=" + SJReqParmas.getString("code");//620100000000
            JSONObject QJHeader = new JSONObject();
            QJHeader.put("Referer", "https://zwfw.gansu.gov.cn/szgs/sxcx/personal/theme/showIndex.do");
            QJHeader.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
            String QJStrResponse = getReq(QJUrl,QJHeader,new JSONObject());
            JSONObject QJResponse = JSONObject.parseObject(QJStrResponse);
            for (Object o1 : QJResponse.getJSONObject("params").getJSONArray("navigationList")){
                JSONObject QJReqParmas = (JSONObject) o1;

                region.replace(SSLength,region.length(),QJReqParmas.getString("name"));
                String areaCode = QJReqParmas.getString("code");


                //获取主题分类列表
                String themeUrl = "https://zwfw.gansu.gov.cn/szgs/sxcx/bmqd/searchTypeAndDeptCorAndPer.do?areaCode="+areaCode+"&flag=2&userType=1";
                JSONObject themeHeader = new JSONObject();
                themeHeader.put("Referer", "https://zwfw.gansu.gov.cn/szgs/sxcx/personal/theme/showIndex.do");
                themeHeader.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
                String themeStrResponse = getReq(themeUrl, themeHeader,new JSONObject());
                JSONObject themeResponse = JSONObject.parseObject(themeStrResponse);
                JSONArray themeList = themeResponse.getJSONObject("params").getJSONArray("themelist");

                //按主题分类
                for (Object theme_ : themeList){
                    JSONObject theme = (JSONObject) theme_;
                    System.out.println(theme);
                    //总页数
                    int pages = 2;
                    //当前页
                    int currentPage = 1;
                    while (pages >= currentPage) {
                        String listUrl = "https://zwfw.gansu.gov.cn/szgs/sxcx/personal/item/showdown.do?" +
                                "gerenthemeId=" + theme.getString("id") +
                                "&deptCode=" +
                                "&isonline=" +
                                "&keyword=" +
                                "&number=" + currentPage +
                                "&numOfOnePage=10" +
                                "&typeId=" +
                                "&areaCode=" + areaCode +
                                "&_=" + System.currentTimeMillis();
                        JSONObject listHeader = new JSONObject();
                        listHeader.put("Referer", "https://zwfw.gansu.gov.cn/szgs/sxcx/personal/theme/showIndex.do?areaCode=" + areaCode);
                        listHeader.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
                        String listResponse = getReq(listUrl, listHeader, new JSONObject());
                        try {
                            Pattern pattern_pages = Pattern.compile("var pageNum=\"(\\S*)\";");
                            Matcher matcher_pages = pattern_pages.matcher(listResponse);
                            while (matcher_pages.find()){
                                pages = Integer.parseInt(matcher_pages.group(1));//总页数
                            }
                        } catch (Exception e) {
                            pages = 1;
                        }

                        JXDocument jxDocument = new JXDocument(listResponse);
                        List<Object> ztsx_list = jxDocument.sel("//ul[@class='ztsx_list_li_son']");
                        for (Object o : ztsx_list) {
                            Elements showBanshizhinans = ((Element) o).getElementsByAttributeValue("class", "clearfix");

                            String url = null;
                            JSONObject header = new JSONObject();
                            String response = null;
                            JSONObject result = new JSONObject();
                            JSONArray pageList = new JSONArray();

                            for (Element showBanshizhinan : showBanshizhinans) {

                                //详情页名称
                                String name = showBanshizhinan.child(0).child(0).text();

                                Pattern pattern = Pattern.compile("<span onclick=\"showBanshizhinan\\('(\\S*)','(\\S*)'\\)\">办事指南</span>");
                                Matcher matcher = pattern.matcher(showBanshizhinan.toString());
                                while (matcher.find()) {
                                    String taskCode = matcher.group(1);
                                    String taskHandleItem = matcher.group(2);
                                    url = "https://zwfw.gansu.gov.cn/szgs/bszn/index.do?taskCode=" + taskCode + "&taskHandleItem=" + taskHandleItem;
                                    header.put("Referer", "https://zwfw.gansu.gov.cn/szgs/sxcx/personal/theme/showIndex.do?areaCode=620102000000");
                                    header.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
                                    response = getReq(url, header, new JSONObject());

                                    //获取详情页信息
                                    JSONObject pageResult = new JSONObject();
                                    //名称
                                    pageResult.put("名称", name);
                                    //详情页地址
                                    pageResult.put("地址", url);
                                    //基本信息
                                    pageResult.put("基本信息", getBaseinfo(response));
                                    //受理条件
                                    pageResult.put("受理条件", getAcceptcondition(response));
                                    //申报材料
                                    pageResult.put("申报材料", getMaterials(response, taskCode, taskHandleItem));
                                    //收费情况
                                    pageResult.put("收费情况", getFeeinfo(response));
                                    //法定依据
                                    pageResult.put("法定依据", getBylaw(response));
                                    //办理流程
                                    pageResult.put("办理流程", getFlowinfo(response, taskCode, taskHandleItem));
                                    //常见问题
                                    pageResult.put("常见问题", getQuestion(response, taskCode, taskHandleItem));

                                    pageList.add(pageResult);
                                }
                            }

                            result.put("名称", ((Element) o).child(0).child(0).child(0).text());
                            result.put("行使层级","区级");
                            result.put("选项", pageList);

                            System.out.println(listUrl + "\n"
                                            + url + "\n"
                                            + region + "\n"
                                            + theme.getString("name") + "\n"
                                            + result + "\n\n"
                            );
//                            return processResult(
//                                    listUrl,
//                                    listResponse,
//                                    url,
//                                    response,
//                                    result,
//                                    region.toString(),
//                                    "个人服务" + theme.getString("name")
//                            );
                        }
                        currentPage++;
                    }
                }
            }

        }

        return null;
    }
    public static void main(String[] args) {
        GSpersonal gs = new GSpersonal();
        gs.getDataDemo();
    }
}
